﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace Kevin.Framework
{
    public static class StringExtendcs
    {
        /// <summary>
        /// 将字符串转换为整数数据
        /// </summary>
        /// <param name="inValue">需转换的字符串</param>
        /// <returns>转换后的整数数字，如转换失败将返回0</returns>
        public static int ToInt(this string inValue)
        {
            int result = 0;
            int.TryParse(inValue, out result);
            return result;
        }
        /// <summary>
        /// 将字符串转换为十进位小数
        /// </summary>
        /// <param name="inValue"></param>
        /// <returns></returns>
        public static decimal ToDecimal(this string inValue)
        {
            decimal result = 0;
            decimal.TryParse(inValue, out result);
            return result;
        }

        /// <summary>
        /// 将字符串转换为 日期时间格式
        /// </summary>
        /// <param name="inValue"></param>
        /// <returns></returns>
        public static DateTime ToDateTime(this string inValue)
        {
            DateTime result = default(DateTime);
            DateTime.TryParse(inValue, out result);
            return result;
        }
        /// <summary>
        /// 将字符串转换为 日期格式
        /// </summary>
        /// <param name="inValue"></param>
        /// <returns></returns>
        public static DateTime ToDate(this string inValue)
        {
            DateTime result = default(DateTime);
            DateTime.TryParseExact(inValue,"yyyy-MM-dd",null,System.Globalization.DateTimeStyles.AssumeLocal, out result);
            return result;
        }
        /// <summary>
        /// 对字符串进行MD5加密
        /// </summary>
        /// <param name="inValue">需加密的字符串</param>
        /// <returns>加密后的字符串</returns>
        public static string ToMD5String(this string inValue)
        {
            // First we need to convert the string into bytes, which
            // means using a text encoder.
            byte[] unicodeText = System.Text.Encoding.UTF8.GetBytes(inValue);

            // Now that we have a byte array we can ask the CSP to hash it
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] result = md5.ComputeHash(unicodeText);

            // Build the final string by converting each byte
            // into hex and appending it to a StringBuilder
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < result.Length; i++)
            {
                sb.Append(result[i].ToString("x2"));
            }

            // And return it
            return sb.ToString();
        }
        /// <summary>
        /// 将16进制的字符串转换为float数据
        /// </summary>
        /// <param name="hexString">需转换的16进制字符串</param>
        /// <returns>float数据</returns>
        public static float HexStr2Float(this String hexString)
        {
            uint num = uint.Parse(hexString, System.Globalization.NumberStyles.AllowHexSpecifier);
            byte[] floatVals = BitConverter.GetBytes(num);
            return BitConverter.ToSingle(floatVals, 0);
        }
    }
}
